$NG.AllReady(function (page, { useUpdateRow, useValuesChange, useBeforeClick, useDataIndexChange }) {
    const mstform = $NG.getCmpApi("p_form_unforeseeable_fb");

    {//初始化逻辑BEGIN
        if (mstform) {
            console.log(mstform.name + ' is ready');
        } else {
            console.error('mstform is not available');
        };
    }//初始化逻辑END

    if (page.oType == "add") {
        var pc = mstform.getItem("phid_pc");
        console.log("123123");
        if (pc.getValue() > "0") {
            pcinfo()
            setTitle()
        }

    }

    //项目名称变化时自动带入产品线、事业部等
    useValuesChange(({ args }) => {
        console.log(args[0]);
        const phid_pc = args[0].phid_pc.value;
        if (phid_pc) {
            console.log(phid_pc);
            pcinfo()
            setTitle()
        }

    }, "p_form_unforeseeable_fb.phid_pc");

    const keys = ["u_amt_vat","u_taxrate","u_jfdw","u_fbdw"]
    keys.forEach(item=>{
        useValuesChange(({ args }) => {
            sumAmtM()
    
        }, "p_form_unforeseeable_fb."+item);
    })



    // useValuesChange(({ args }) => {
    //     var bczb = mstform.getItem('u_unforeseeable_zdys').getValue();
    //     var yzb = mstform.getItem('u_unforeseeable_yfpzd').getValue();
    //     var zdfs = mstform.getItem('u_unforeseeable_zdfs').getValue();

    //     mstform.getItem('u_unforeseeable_ljzd').setValue(bczb + yzb)
    //     mstform.getItem('u_unforeseeable_syzd').setValue(bczb + yzb - zdfs)

    // }, "p_form_unforeseeable_fb.u_unforeseeable_zdys");

    // useValuesChange(({ args }) => {
    //     var bcfb = mstform.getItem('u_unforeseeable_fbys').getValue();
    //     var yfb = mstform.getItem('u_unforeseeable_yfpfb').getValue();
    //     var fbfs = mstform.getItem('u_unforeseeable_fbfs').getValue();

    //     mstform.getItem('u_unforeseeable_ljfb').setValue(bcfb + yfb)
    //     mstform.getItem('u_unforeseeable_syfb').setValue(bcfb + yfb - fbfs)

    // }, "p_form_unforeseeable_fb.u_unforeseeable_fbys");



    useBeforeClick(async function ({ args }) {

        var u_amt_vat = mstform.getItem('u_amt_vat').getValue();
        var syje = mstform.getItem('u_unforeseeable_fbsy').getValue();
        var fbys = mstform.getItem('u_unforeseeable_fbys').getValue();

        if (u_amt_vat > syje) {
            $NG.alert("已超出不可预见费用分包预算，请再次分配分包预算金额")
            return false
        }

        else {
            return true
        }


    }, "save");


    function pcinfo() {
        var pcname = mstform.getItem("phid_pc").getRawValue().label

        $NG.execServer('getPtInfoByPhid', { phid: mstform.getItem("phid_pc").getValue() }, function (res) {
            const datas = JSON.parse(res.data)[0].extendObjects;
            console.log(datas);
            $NG.updateState((updater) => {
                //【注意这里的表名不能为控件ID，必须要是表名】
                updater.data.p_form_unforeseeable_fb.setProps({
                    u_division: {
                        value: datas.division_phid,
                        label: datas.division_name,
                    },
                    phid_org: {
                        value: datas.org_phid,
                        label: datas.org_name,
                    },
                    u_regional_center: {
                        value: datas.regional_center_phid,
                        label: datas.regional_center_name,
                    },
                    u_industry_line: {
                        value: datas.midwife_phid,
                        label: datas.midwife_name,
                    },
                    u_jfdw: {
                        value: datas.cwhszz,
                        label: datas.cwzz_name,
                    },
                    u_kpdw: {
                        value: datas.cwhszz,
                        label: datas.cwzz_name,
                    },
                    u_qyzz: {
                        value: datas.cwhszz,
                        label: datas.cwzz_name,
                    },
                    u_pc_no: datas.pc_no,

                });
            });
        });

        $NG.execServer("GetSbUnforeseeable_Info", { pc: mstform.getItem('phid_pc').getValue() }, function (res) {
            if(res.count == "0") return
            if(res.count == "1") {
                const data = JSON.parse(res.data);
                const u_unforeseeable_fbys = data[0].extendObjects.u_unforeseeable_fbys;
                const u_unforeseeable_syfb = data[0].extendObjects.u_unforeseeable_syfb;
                $NG.updateState((updater) => {
                    updater.data.p_form_unforeseeable_fb.setProps({
                        u_unforeseeable_fbys: u_unforeseeable_fbys,
                        u_unforeseeable_fbsy: u_unforeseeable_syfb,
                    });
                });
                            
            }
        });
    }
    function setTitle() {
        var pcname = mstform.getItem("phid_pc").getRawValue().label

        $NG.updateState((updater) => {
            updater.data.p_form_unforeseeable_fb.setProps({
                u_cnt_name: pcname,
            });
        });

    }
    // 计算表头
    page.oType == 'add' && sumAmtM()
    function sumAmtM(u_amt_vat,u_taxrate){
        const amt = mstform.getItem("u_amt_vat").getValue()
        const taxrate = mstform.getItem("u_taxrate").getValue()
        $NG.updateState((updater)=>{
            updater.data.p_form_unforeseeable_fb.setProps({
                amt:amt * (1 - taxrate),
                u_tax_amt:amt - (amt * (1 - taxrate)),
                u_kpdw:{
                    value:mstform.getItem("u_fbdw").getValue(),
                    label:mstform.getItem("u_fbdw").getText(),
                },
                u_spdw:{
                    value:mstform.getItem("u_jfdw").getValue(),
                    label:mstform.getItem("u_jfdw").getText(),
                },
            })
        })
    }

});